package com.File;

import cn.hutool.core.io.FileUtil;
import com.luciad.imageio.webp.WebPReadParam;

import javax.imageio.ImageIO;
import javax.imageio.ImageReader;
import javax.imageio.stream.FileImageInputStream;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

public class WebpToJpg {

    public static void main(String[] args) {
        String a = "url";
        File[] files = FileUtil.ls(a);
        for(File file : files){
            System.out.println(file.getPath());
            WebpToJpg.ReplaceWebpToPng(file.getPath());
        }

    }


    public static void ReplaceWebpToPng(String dirPath){
        File[] files = FileUtil.ls(dirPath);
        if (files != null){
            for(File f : files){
                if (!getExtensionName(f.getName()).equals("webp")){
                    continue;
                }
                //System.out.println("old:"+f.getName());
                //获取文件名
                String filename = f.getName().substring(0,f.getName().length()-5);
                //System.out.println(filename);
                String newFilename = filename+".png";
                RenameFile(dirPath,f.getName(),newFilename);
            }
        }
    }

    public static void RenameFile(String path,String filename,String newFilename){
        String oldFilename = path+File.separator+filename;
        File oldFile = new File(oldFilename);
        String newName = path+File.separator+newFilename;
        File newFile = new File(newName);
        if (oldFile.exists() && oldFile.isFile()){
            oldFile.renameTo(newFile);
        }
    }

    /**
     * 当前目录下，所有webp文件转png，png到指定目录
     * @param sourceAddress
     * @param addr
     */
    public void allWebpToPng(String sourceAddress, String addr) throws IOException {
        File[] files = FileUtil.ls(sourceAddress);
        FileUtil.mkdir(addr);
        int count = 0; //统计转换了多少文件
        for (File file :
                files) {
            if (!getExtensionName(file.getName()).equals("webp")){
                //后缀名不是webp，就跳过
                continue;
            }
            String newName = file.getName();
            File pngFile = webpToPng(file, addr);
            count++;
        }
        System.out.println(count);
    }


    /**
     * 获取文件扩展名
     */
    public static String getExtensionName(String filename) {
        if ((filename != null) && (filename.length() > 0)) {
            int dot = filename.lastIndexOf('.');
            if ((dot >-1) && (dot < (filename.length() - 1))) {
                return filename.substring(dot + 1);
            }
        }
        return filename;
    }


    /**
     * 图片webp文件转png文件
     * @param webpFile 源webp文件
     * @param address 输出地址
     * @return
     * @throws IOException
     */
    public static File webpToPng(File webpFile, String address) throws IOException {
        // Obtain a WebP ImageReader instance
        ImageReader reader = ImageIO.getImageReadersByMIMEType("com/image/webp").next();

        // Configure decoding parameters
        WebPReadParam readParam = new WebPReadParam();
        readParam.setBypassFiltering(true);

        // Configure the input on the ImageReader
        reader.setInput(new FileImageInputStream(webpFile));

        // Decode the image
        BufferedImage image = reader.read(0, readParam);

        //the `png` can use `jpg`
        File pngFile = new File(address);
        ImageIO.write(image, "png", pngFile);
        return pngFile;
    }
}
